# -*- coding:utf-8 -*-

from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.table2JSON import Table2Json


class IndustryDao(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections()

    def add_industry(self, indus_info):
        industry_name = indus_info['industry_name']
        industry_vid = indus_info['industry_vid']
        industry_code = indus_info['industry_code']
        industry_status = indus_info['industry_status']
        industry_sort = indus_info['industry_sort']

        cur = self.conn.cursor()
        sql = "insert into zzh_industry (industry_name,industry_vid,industry_code,industry_status,industry_sort) " \
              "VALUES (%s,%s,%s,%s,%s)"
        result = cur.execute(sql, [industry_name, industry_vid, industry_code, industry_status, industry_sort])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return result

    def update_indus(self, indus_id, indus_info):
        industry_name = indus_info['industry_name']
        industry_vid = indus_info['industry_vid']
        industry_code = indus_info['industry_code']
        industry_status = indus_info['industry_status']
        industry_sort = indus_info['industry_sort']

        cur = self.conn.cursor()
        sql = "update zzh_industry set industry_name=%s,industry_vid=%s,industry_code=%s,industry_status=%s,industry_sort=%s " \
              "WHERE industry_id=%s "
        result = cur.execute(sql, [industry_name, industry_vid, industry_code, industry_status, industry_sort, indus_id])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return result

    def query_indus(self):
        cur = self.conn.cursor()

        sql = "select * from zzh_industry"
        rs = cur.execute(sql)
        indus_list = cur.fetchall()
        indus_json = Table2Json.indus_list_to_json(indus_list)
        cur.close()
        self.conn.close()
        return indus_json


